﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using LukasKopenec.LocalizationSupport;
using Notoric.ViewModel;

namespace Notoric.Gui.Windows
{
    /// <summary>
    /// Allows user to select a picture from project resources.
    /// </summary>
    public partial class SelectPictureFromResourcesDialog : Window
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SelectPictureFromResourcesDialog"/> class.
        /// </summary>
        public SelectPictureFromResourcesDialog()
        {
            InitializeComponent();

            PART_ResourcesTree.Items.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles <see cref="IProjectExplorerItem.Children"/> collection filtering.
        /// </summary>
        /// <param name="sender">Object that has sent the event</param>
        /// <param name="e">Event data</param>
        private void ChildrenCollection_Filter(object sender, FilterEventArgs e)
        {
            var peItem = (IProjectExplorerItem)e.Item;
            if (peItem is Directory)
                e.Accepted = true;
            else
            {
                if (peItem is ViewModel.Elements.Resource)
                {
                    var extension = ((ViewModel.Elements.Resource)peItem).Extension;
                    var imageFilter = (string)LocalizationManager.GetValue("Application.FileFilters.ImageFiles");

                    e.Accepted = imageFilter.Contains(extension);
                }
                else
                    e.Accepted = false;
            }
        }

        /// <summary>
        /// Occurs when the selected item in the resources tree has changed.
        /// </summary>
        /// <param name="sender">Object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void PART_ResourcesTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (PART_ResourcesTree.SelectedItem == null || !(PART_ResourcesTree.SelectedItem is ViewModel.Elements.Resource))
                PART_OK.IsEnabled = false;
            else
                PART_OK.IsEnabled = true;
        }

        /// <summary>
        /// Occurs when the user clicks the OK button.
        /// </summary>
        /// <param name="sender">Object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void PART_OK_Click(object sender, RoutedEventArgs e)
        {
            SelectedResource = PART_ResourcesTree.SelectedItem as ViewModel.Elements.Resource;
            this.DialogResult = true;
        }

        /// <summary>
        /// Occurs when the user clicks the Cancel button.
        /// </summary>
        /// <param name="sender">Object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void PART_Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        #endregion

        #region Dependency Properties Identifiers

        /// <summary>
        /// Identifies the <see cref="ResourcesRoot"/> dependency property.
        /// </summary>
        public static readonly DependencyProperty ResourcesRootProperty = DependencyProperty.Register(
            "ResourcesRoot", typeof(IProjectExplorerItem), typeof(SelectPictureFromResourcesDialog)
            );

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the root of the resource hierarchy to select picture from.
        /// </summary>
        /// <value>
        /// <para>
        /// Type: <see cref="IProjectExplorerItem"/>
        /// </para>
        /// <para>
        /// The root of the resource hierarchy.
        /// </para>
        /// </value>
        public IProjectExplorerItem ResourcesRoot
        {
            get { return (IProjectExplorerItem)GetValue(ResourcesRootProperty); }
            set { SetValue(ResourcesRootProperty, value); }
        }

        /// <summary>
        /// Gets the resource that the user has selected.
        /// </summary>
        /// <value>
        /// <para>
        /// Type: <see cref="ViewModel.Elements.Resource"/>
        /// </para>
        /// <para>
        /// The resource that the user has selected.
        /// This value is defined only if the user has confirmed
        /// the dialog (<see cref="Window.ShowDialog"/> returns <see langword="true"/>).
        /// </para>
        /// </value>
        public ViewModel.Elements.Resource SelectedResource
        {
            get;
            private set;
        }

        #endregion
    }
}
